<template>
	<!-- <my-search></my-search> -->
	<view class="scroll_container">
		<scroll-view class="left_scroll" scroll-y="true" :style="{height: windowH + 'px'}">
			<view :class="['left_scroll_itme', i === active ? 'active' : '' ]" v-for="(cateModel, i) in categoryList" :key="i" @click="selectClick(item,i)">
				{{cateModel.cat_name}}
			</view>
		</scroll-view>
		<!--flex: 1 让所有弹性盒模型对象的子元素都有相同的长度，且忽略它们内部的内容：
		ps:如果不用view标签，直接scroll-view就会自动撑起，不用设置flex:1, 因为scroll-view有默认宽度100%
		-->
		<view style="flex: 1">
			<scroll-view class="right_scroll" scroll-y="true" :style="{height: windowH + 'px'}" :scroll-top="scrollTop">
				<view class="right_scroll_item" v-for="(item2, i) in cateLevel2" :key="i">
					<!--二级分类-->
					<view class="level2_title">/ {{item2.cat_name}} /</view>
						<!--三级内容-->
					<view class="level3_litst">
						<view class="level3_item" v-for="(item3, index) in item2.children" :key="index" @click="level3ItemClick(item3.cat_id)">
							<image src="../../static/cart_empty@2x.png"></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		
		data() {
			return {
				windowH: 0,
				categoryList: [],
				active: 0,
				cateLevel2: [],
				scrollTop: 0
			}
		},
		
		onLoad() {
			//获取屏幕可用高度
			const sysInfo = uni.getSystemInfoSync()
			this.windowH = sysInfo.windowHeight
			
			this.getCateList()
		},
		
		methods: {
			// 获取分类列表的数据
			async getCateList() {
			  const { data: res } = await uni.$http.get('/api/public/v1/categories')
			  if (res.meta.status !== 200) return uni.$showMsg()
			  this.categoryList = res.message
			
			  // 为二级分类赋值
			  this.cateLevel2 = res.message[0].children
			},
			
			selectClick(categoryModel, index) {
				this.active = index
				//重新为二级分类赋值
				this.cateLevel2 = this.categoryList[index].children
				//如果scrollTop赋值每次都是同一个值，动态绑定是不会起作用，所以这里用三目运算符处理一下
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			
			level3ItemClick(id) {
				uni.navigateTo({
					url:'../../subpkg/goods_detail/goods_detail?id=' + id
				})
			}
		}
	}
		
</script>

<style lang="scss">
	
	.scroll_container {
		display: flex;
	}
	
	.left_scroll {
		width: 120px;
	}
	
	.left_scroll_itme {
		width: 100%;
		line-height: 50px;
		text-align: center;
		background-color: #F7F7F7;
		/* &代表是父亲 */
		&.active {
			background-color: #FFFFFF;
			position: relative; /*相对于父亲位置是动态*/
			
			/*在父亲标签内的前面插入内容*/
			&::before {
				content: '';
				background-color: #c00000;
				display: block;
				width: 3px;
				height: 30px;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
			}
			
		}
	}

	.level2_title {
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}
	
	.level3_litst {
		display: flex;
		flex-wrap: wrap;
		
		.level3_item {
			width: 33.3%;
			display: flex;
			flex-direction: column;
			justify-content: center; //横轴居中
			align-items: center; //纵轴居中
			margin-bottom: 10px;
			
			image {
				width: 60px;
				height: 60px;
			}
			text {
				font-size: 12px;
			}
		}
	}
	
	
	
</style>
